# libs ----
library(sf)
library(geomander)
library(tidyverse)
library(redist)
library(ppmf)
library(grid)
library(patchwork)
library(wacolors)

# consts ----

state <- 'LA'
# get data ----

# data ----
ppmf19_path <- "../../data-raw/ppmf_19.csv"
ppmf12_path <- "../../data-raw/ppmf_12.csv"
ppmf4_path <- "../../data-raw/ppmf_04.csv"

ppmf19 <- read_ppmf(state, ppmf19_path)
ppmf12 <- read_ppmf(state, ppmf12_path)
ppmf4 <- read_ppmf(state, ppmf4_path)

ppmf19 <- ppmf19 %>% add_geoid() %>% agg() %>% breakdown_geoid()
colnames(ppmf19) <- paste("v19", colnames(ppmf19), sep = "_")
ppmf19 <- ppmf19 %>% rename(GEOID = v19_GEOID)

ppmf12 <- ppmf12 %>% add_geoid() %>% agg() %>% breakdown_geoid()
colnames(ppmf12) <- paste("v12", colnames(ppmf12), sep = "_")
ppmf12 <- ppmf12 %>% rename(GEOID = v12_GEOID)

ppmf4 <- ppmf4 %>% add_geoid() %>% agg() %>% breakdown_geoid()
colnames(ppmf4) <- paste("v4", colnames(ppmf4), sep = "_")
ppmf4 <- ppmf4 %>% rename(GEOID = v4_GEOID)

# comparison ----
census <- create_block_table(state = state)

# all joined ----
block <- census %>%
  left_join(ppmf12, by = 'GEOID') %>%
  left_join(ppmf4, by = 'GEOID') %>% 
  left_join(ppmf19, by = "GEOID")

# and remove duplicates
block <- block %>% select(-contains('.'))

# and set missing block pop/vap to 0
block[is.na(block)] <- 0

# add block_group back (dropped by contains('.'))
block <- block %>% breakdown_geoid()
sld_up <- tigris::state_legislative_districts(state, 'upper')

# match ----
block_sld_up_match <- geo_match(from = block, to = sld_up, method = 'centroid')

# Add to data ----
block <- block %>%
  mutate(sld_up = sld_up$SLDUST[block_sld_up_match])

# clean data ----
dist <- block %>%
  st_drop_geometry() %>%
  filter(sld_up != 'ZZZ') %>% # Remove Big Lake
  group_by(sld_up) %>%
  summarize(pop = sum(pop),
            pop4 = sum(v4_pop),
            pop12 = sum(v12_pop),
            pop19 = sum(v19_pop))

# parity ----
en_p <- redist.parity(as.numeric(dist$sld_up), dist$pop)
v4_p <- redist.parity(as.numeric(dist$sld_up), dist$pop4)
v12_p <- redist.parity(as.numeric(dist$sld_up), dist$pop12)
v19_p <- redist.parity(as.numeric(dist$sld_up), dist$pop19)

parity_lines <- tibble(int = c(en_p, v4_p, v12_p, v19_p),
                dataset = c("Sampled from: Census",
                            "Sampled from: DAS-4.5",
                            "Sampled from: DAS-12.2",
                            "Sampled from: DAS-19.61"),
                name = c("Census", "DAS-4.5", "DAS-12.2", "DAS-19.61"))
